/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi;

import com.inspur.edp.bff.api.exception.BffFrameworkException;
import com.inspur.edp.bff.api.exception.ErrorCodes;
import com.inspur.edp.bff.spi.utils.VoMapIgnoreKeysUtil;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefEntityResInfoImpl;
import com.inspur.edp.cef.spi.jsonser.entity.AbstractEntitySerializerItem;

import java.util.ArrayList;

/**
 * BE序列化类
 */
public class BffEntityDeserConvertor extends AbstractBffEntityDeserConvertor {
    private final String type;
    private final CefEntityResInfoImpl info;

    public BffEntityDeserConvertor(String objectCode, boolean isRoot, String voType, CefEntityResInfoImpl resourceInfo) {
        super(objectCode, isRoot, voType, getTypes(resourceInfo));
        type = voType;
        this.info = resourceInfo;
    }

    private static ArrayList<AbstractEntitySerializerItem> getTypes(CefEntityResInfoImpl resourceInfo) {
        ArrayList<AbstractEntitySerializerItem> list = new ArrayList<>();
        list.add(new com.inspur.edp.cef.spi.jsonser.builtinimpls.CefEntityDataSerializerItem(resourceInfo));
        return list;
    }

    @Override
    protected AbstractBffEntityDeserConvertor getChildConvertor(String childCode) {
        if (info.getChildEntityResInfos() != null && VoMapIgnoreKeysUtil.containsIgnoreKey(info.getChildEntityResInfos().keySet(), childCode)) {
            return new BffEntityDeserConvertor(VoMapIgnoreKeysUtil.getRelKey(info.getChildEntityResInfos().keySet(), childCode), false, type, (CefEntityResInfoImpl) VoMapIgnoreKeysUtil.getValueByIgnoreKey(info.getChildEntityResInfos(), childCode));
        }
        throw new BffFrameworkException(ErrorCodes.BFF_FRAMEWORK_2010, childCode);
    }
}
